//
//  HJVisitorLoginView.swift
//  HJWeiBo
//
//  Created by 夏航军 on 16/9/23.
//  Copyright © 2016年 夏航军. All rights reserved.
//

import UIKit
import SnapKit

//
 @objc protocol HJVisitorLoginViewDelegate : NSObjectProtocol {
    
    //登录
    func userWillLogin()
    
    //注册
  @objc optional func userWillReigster()
}



class HJVisitorLoginView: UIView {

    
    //设置不同控制器登录界面的内容
    func userInfo(tipText : String,imageName : String? = nil){
        //更新子控制器的属性
        tipLable.text = tipText
        
        if imageName == nil {
            //判断如果是首页就开启动画效果
            startAnimation()
            
        }else{
            circleView.image = UIImage(named: imageName!)
            backView.isHidden = true
            iconView.isHidden = true;
            
            bringSubview(toFront: circleView)
        }
    }
    
    
    //Mark:==========================动画效果===============================
    func startAnimation() {
        //使用基本动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画重复次数
        animation.repeatCount = MAXFLOAT
        //一圈
        animation.toValue = M_PI * 2
         //动画时间
        animation.duration = 15
        //当动画结束时候或者当页面处于非活动状态时移除动画效果,默认值为true
        animation.isRemovedOnCompletion = false
        //将动画效果添加到图层上
        circleView.layer.add(animation, forKey: nil)
    }
    
    
    //申明代理对象属性
    var delegate : HJVisitorLoginViewDelegate?
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //Mark:创建子控件
    func setupUI() {
       addSubview(circleView)
        addSubview(iconView)
        addSubview(backView)
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
        iconView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        tipLable.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(10)
            make.centerX.equalTo(circleView)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(tipLable)
            make.top.equalTo(tipLable.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLable)
            make.top.equalTo(tipLable.snp.bottom).offset(16)
            make.width.equalTo(100)
        }

        let value = CGFloat(237 / 255.0)
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(registerBtnDidClick), for: .touchUpInside)
    }
    
    
    
    func loginBtnDidClick() {
        //使用代理对象调用协议方法
        //判断是否为为nil,是否响应方法
        delegate?.userWillLogin()
        
    }
    
    
    func registerBtnDidClick() {
        
        delegate?.userWillReigster?()
    }
    
    //Mark:----------------------------------懒加载控件-------------------------------------
    //圆圈
    lazy var circleView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //大房子
    lazy var iconView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    //遮罩
    lazy var backView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    //提示文字
    lazy var tipLable : UILabel = {
        //lable分类
        var lable : UILabel = UILabel(title: "你点一下试试,试一下就有惊喜的.快点", textColor: UIColor.darkGray, fontSize: 14)
        
        //换行
        lable.numberOfLines = 0
        //Mark:设置最大的布局宽度
        lable.preferredMaxLayoutWidth = 224
        //文字居中
        lable.textAlignment = .center

        return lable
    }()
    
    //登录按钮
    lazy var loginBtn : UIButton = {
        var btn : UIButton = UIButton(title: "登录", textColor: UIColor.darkGray, fontSize: 14)
        //按钮图片
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
    
    //注册按钮
    lazy var registerBtn : UIButton = {
        var btn : UIButton = UIButton(title: "注册", textColor: UIColor.orange, fontSize: 14)
        //按钮图片
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()

}











